package com.openxt.nbzj.plugins;

import android.content.Intent;
import android.os.Handler;

import com.openxt.nbzj.activity.EnterpriseCreditActivity;
import com.openxt.nbzj.activity.EnterpriseGetNativeActivity;
import com.openxt.nbzj.activity.HomeActivity;
import com.openxt.nbzj.activity.LoginActivity;
import com.openxt.nbzj.activity.NewsActivity;
import com.openxt.nbzj.activity.NoticeActivity;
import com.openxt.nbzj.activity.ProjectActivity;
import com.openxt.nbzj.call.HeartBeatManager;
import com.openxt.nbzj.model.ConfigModel;
import com.openxt.nbzj.remoting.ClientEx;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;

import qs.openxt.libs.util.StringUtil;
import qs.openxt.libs.util.Utils;

/**
 * Created by 颖杰_2 on 2016-07-16.
 */
public class AppConfigModelPlugin extends CordovaPlugin{
    private CallbackContext callbackContext = null;
    Handler mHandler = new Handler();

    public boolean execute(final String action, final JSONArray args, final CallbackContext callbackContext) throws JSONException {
        this.callbackContext = callbackContext;
        if ("doLoginoutApp".equals(action)) {
            ClientEx.CUSTOMERID = null;
            Utils.removeLocalStorage(cordova.getActivity(), ClientEx.CUSTOMERID_KEY);
            Utils.removeLocalStorage(cordova.getActivity(), ConfigModel.LOGIN_NATIVE);

            HeartBeatManager.instance().stop();
        }else if ("doGoPageApp".equals(action)) {
            String pageName = args.getString(0);
            if(StringUtil.isNull(pageName))
                pageName = Utils.getLocalStorage(cordova.getActivity(), ConfigModel.NATIVE_PAGENAME);
            final String $pageName = pageName;
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if(!ConfigModel.NATIVE_LIST.contains($pageName))
                        return;
                    /*
                    if("#/".equals($pageName)){
                        Intent intent = new Intent();
                        intent.setClass(cordova.getActivity(), HomeActivity.class);
                        cordova.getActivity().startActivity(intent);
                        cordova.getActivity().finish();
                    }else if("#/login".equals($pageName)){
                        Intent intent = new Intent();
                        intent.setClass(cordova.getActivity(), LoginActivity.class);
                        cordova.getActivity().startActivity(intent);
                        cordova.getActivity().finish();
                    }else if("#/newslist".equals($pageName)){
                        Intent intent = new Intent();
                        intent.setClass(cordova.getActivity(), NewsActivity.class);
                        cordova.getActivity().startActivity(intent);
                        cordova.getActivity().finish();
                    }else if("#/enterprice".equals($pageName)){
                        Intent intent = new Intent();
                        intent.setClass(cordova.getActivity(), EnterpriseGetNativeActivity.class);
                        cordova.getActivity().startActivity(intent);
                        cordova.getActivity().finish();
                    }else if("#/enterpricecredit".equals($pageName)){
                        Intent intent = new Intent();
                        intent.setClass(cordova.getActivity(), EnterpriseCreditActivity.class);
                        cordova.getActivity().startActivity(intent);
                        cordova.getActivity().finish();
                    }else if("#/project".equals($pageName)){
                        Intent intent = new Intent();
                        intent.setClass(cordova.getActivity(), ProjectActivity.class);
                        cordova.getActivity().startActivity(intent);
                        cordova.getActivity().finish();
                    }else if("#/noticelist".equals($pageName)){
                        Intent intent = new Intent();
                        intent.setClass(cordova.getActivity(), NoticeActivity.class);
                        cordova.getActivity().startActivity(intent);
                        cordova.getActivity().finish();
                    }else {
                        Intent intent = new Intent();
                        intent.setClass(cordova.getActivity(), HomeActivity.class);
                        cordova.getActivity().startActivity(intent);
                        cordova.getActivity().finish();
                    }*/
                    cordova.getActivity().finish();
                }
            }, 0);
        }else if("doPageApp".equals(action)){
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    callbackContext.success(Utils.getLocalStorage(cordova.getActivity(), ConfigModel.NATIVE_PAGENAME));
                }
            }, 0);
            return true;
        }else if ("doLoginInfoApp".equals(action)) {
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    callbackContext.success(Utils.getLocalStorage(cordova.getActivity(), ConfigModel.LOGIN_NATIVE));
                }
            }, 0);
            return true;
        }
        return false;
    }
}
